<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 商铺成员
 */

class Users_mdl extends CI_Model {

    const TABLE = 'users';

    //查询子用户用的
    static public $treeList = array(); 

    public function select(){
        return $this->db->get(self::TABLE)->result_array();
    }

    public function get_one(){
        return $this->db->get(self::TABLE)->row_array();
    }

    //统计
    public function get_count(){
        return $this->db->count_all_results(self::TABLE);
    }

    //添加
    public function create($data){
        $data['login_time'] = $data['create_time'] = time();
        return $this->db->insert(self::TABLE,$data);
    }
    //修改
    public function update($data){
        return $this->db->update(self::TABLE,$data);
    }
    //删除
    public function delete(){
        $data = ['status' => 2];
        return $this->update($data);
    }

    //数据组装
    public function restr($list){

        $this->load->model('level_mdl');
        $this->load->model('bull_mdl');
        $this->load->model('shops_mdl');
        
        foreach ($list as $k => $v){
            //权限
            if(isset($v['level_id'])){
                if(empty($level)){
                    $this->db->where('group_id !=',0);
                    $level = $this->level_mdl->select();
                }

                foreach($level as $key => $value){
                    if($v['level_id'] == $value['group_id']){
                        $list[$k]['level'] = $value;
                    }
                }

                if(empty($list[$k]['level'])){
                    $list[$k]['level'] = '普通会员';
                }

                if($v['group'] == 1){
                    $list[$k]['level'] = '管理员';
                }

            }

            //统计商铺
            $this->db->where('uid',$v['user_id']);
            $list[$k]['count'] = $this->bull_mdl->get_count();

            //统计总共金额
            $this->db->where('uid',$v['user_id']);
            $get_sum = $this->bull_mdl->get_sum();
            $list[$k]['bull'] = empty($get_sum['bull']) ? 0 : $get_sum['bull'];

            //缴费店铺
            $this->db->where('uid',$v['user_id']);
            $list[$k]['s_count'] = $this->shops_mdl->get_count();

        }

       // print_r($list);

        return $list;
    }

    //递归查询上级
    public function get_superior($id,$data=array()){
        if(empty($data)){
            $this->db->select('id,user_pid,level_id,user_id,username,user_name');
            $data = $this->select();
        }
        
        foreach($data as $k => $v){
            if($id == $v['user_id']){
                self::$treeList[] = $v;
                unset($data[$k]);
                $this->get_superior($v['user_pid'],$data);
            }
        }

        return self::$treeList;
    }

    //递归查询用户下级
    public function get_lower($id,$data=array()){
        if(empty($data)){
            $this->db->select('id,user_pid,level_id,user_id,username,user_name');
            $data = $this->select();
        }

        foreach($data as $k => $v){
            if($id == $v['user_pid']){
                self::$treeList[] = $v;
                unset($data[$k]);
                $this->get_superior($v['id'],$data);
            }
        }
        
        return self::$treeList;
    }
    
}